import Taro from "@tarojs/taro";
import { serverDomain, port } from "../config/config";

import { CHECK_PLAN, CHECK_TODAY_PLAN,SET_CURR_MISSION } from "../constants/checkPlan";

export function onCheckPlan(data) {
  return dispatch => {
    Taro.showToast({
        title:'获取计划...',
        icon:'loading',
        mask:true
    })
        .then(()=>{
            Taro.request({
                url: `${serverDomain}:${port}/plan/getPlan`,
                method: "GET",
                data: data,
                success: res => {
                  dispatch({
                    type: CHECK_PLAN,
                    payload: res
                  });
                },
                fail: () => {
                  Taro.atMessage({
                    message: "服务器出错",
                    type: "error",
                    duration:3000
                  });
                },
                complete:Taro.hideToast
              });
        })
    
  };
}

export function onCheckTodayPlan(data,cb) {
 
  return dispatch => {
    Taro.showToast({
        title:'获取今日计划...',
        icon:'loading',
        mask:true
    })
        .then(()=>{
            Taro.request({
                url: `${serverDomain}:${port}/plan/getPlan`,
                method: "GET",
                data: data,
                success: res => {
              
                  cb&&cb();
                  dispatch({
                    type: CHECK_TODAY_PLAN,
                    payload: res
                  });
                 
                },
                fail: () => {
                  Taro.atMessage({
                    message: "服务器出错",
                    type: "error",
                    duration:3000
                  });
                },
                complete:Taro.hideToast
              });
        })
    
  };
}

export function onSetCurrentMission(mission){
    return {
        type:SET_CURR_MISSION,
        payload:mission
    }
}